package org.jsola.hr.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.excel.EasyExcel;
import org.jsola.admin.query.HeaderFieldsQuery;
import org.jsola.admin.service.IHeaderFieldsService;
import org.jsola.admin.vo.HeaderFieldsListVO;
import org.jsola.admin.vo.HeaderFieldsVO;
import org.jsola.core.Page;
import org.jsola.hr.common.BeanCopyUtil;
import org.jsola.exception.ParamException;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.FormGroupModuleCode;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dao.IFormGroupDAO;
import org.jsola.hr.dao.IGysInfoDAO;
import org.jsola.hr.dto.*;
import org.jsola.hr.entity.FormGroupFieldValueDO;
import org.jsola.hr.dto.GysInfoAddDTO;
import org.jsola.hr.dto.GysInfoUpdateDTO;
import org.jsola.hr.dto.excel.GysInfoImportDTO;
import org.jsola.hr.entity.GysInfoDO;
import org.jsola.hr.query.ExportColumn;
import org.jsola.hr.query.FormGroupFieldValueQuery;
import org.jsola.hr.query.GysInfoQuery;
import org.jsola.hr.service.IFormGroupFieldValueService;
import org.jsola.hr.service.IFormGroupService;
import org.jsola.hr.service.IGysInfoService;
import org.jsola.hr.vo.*;
import org.jsola.hr.service.excel.GysInfoImportListener;
import org.jsola.hr.vo.excel.GysInfoExportVO;
import org.jsola.hr.vo.excel.ParseResultVO;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 供应商信息表
 *
 * @author zhr
 */
@Slf4j
@Service("hrGysInfoServiceImpl")
public class GysInfoServiceImpl implements IGysInfoService {

    @Autowired
    private IGysInfoDAO gysInfoDAO;

    @Autowired
    private IFormGroupFieldValueService formGroupFieldValueService;

    @Autowired
    private IFormGroupDAO formGroupDAO;

    @Autowired
    private IHeaderFieldsService headerFieldsService;

    @Autowired
    private IFormGroupService formGroupService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public GysInfoVO save(GysInfoAddDTO gysInfoAddDTO, TokenUser tokenUser) {
        // 转DO
        GysInfoDO gysInfoDO = gysInfoAddDTO.to(GysInfoDO.class);
        // 保存
        gysInfoDO = save(gysInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());

        return gysInfoDO.to(GysInfoVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public GysInfoVO saveGsyInfo(GysAddDTO gysAddDTO, TokenUser tokenUser) {

        String groupId = gysAddDTO.getGroupId();
        List<GysInfoFieldAddDTO> list = gysAddDTO.getList();

        //供应商系统字段
        Map map = new HashMap();

        //获取自定义字段，保存自定义字段的值
        List<FormGroupFieldValueAddDTO> valueList = new ArrayList<FormGroupFieldValueAddDTO>();

        for (GysInfoFieldAddDTO gysInfoFieldAddDTO : list) {
            String enName = gysInfoFieldAddDTO.getEnName();
            if (StringUtils.isNotBlank(enName)) {
                map.put(enName, gysInfoFieldAddDTO.getContent());
            }
        }

        //保存供应商主表
        GysInfoDO gysInfoDO = new GysInfoDO();
        try {
            BeanUtils.populate(gysInfoDO, map);
        } catch (Exception e) {
            log.error("map 转DO异常：", e);
        }
        //校验供应商名称不能重复
        String gysName = gysInfoDO.getGysName();
        GysInfoQuery gysInfoQuery = new GysInfoQuery();
        gysInfoQuery.setGysName(gysName);
        List<GysInfoListVO> gysList = select(gysInfoQuery, tokenUser.getSiteId());
        if (!CollectionUtils.isEmpty(gysList)) {
            throw new ParamException("供应商名称已存在");
        }

        gysInfoDO.setGroupId(groupId);
        gysInfoDO = save(gysInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());

        for (GysInfoFieldAddDTO gysInfoFieldAddDTO : list) {
            String enName = gysInfoFieldAddDTO.getEnName();
            if (StringUtils.isBlank(enName)) {
                FormGroupFieldValueAddDTO formGroupFieldValueAddDTO = new FormGroupFieldValueAddDTO();
                formGroupFieldValueAddDTO.setFieldId(gysInfoFieldAddDTO.getFieldId());
                formGroupFieldValueAddDTO.setContent(gysInfoFieldAddDTO.getContent());
                formGroupFieldValueAddDTO.setSubjectId(gysInfoDO.getId());
                formGroupFieldValueAddDTO.setSubjectType("gys");
                valueList.add(formGroupFieldValueAddDTO);
            }
        }

        //保存自定义字段
        if (!CollectionUtils.isEmpty(valueList)) {
            formGroupFieldValueService.batchSave(valueList, tokenUser);
        }
        return gysInfoDO.to(GysInfoVO.class);
    }

    @Override
    public List<FormGroupVO> findGsyById(String gysInfoId, String siteId) {
        //查询模板
        List<FormGroupVO> form = formGroupDAO.findForm(siteId);
        //系统字段
        GysInfoDO gysInfoDO = gysInfoDAO.selectByPrimaryKey(gysInfoId);
        Map<String,Object> map = new HashMap<>();
        try {
            map = BeanCopyUtil.objectToMap(gysInfoDO);
        } catch (IllegalAccessException e) {
            log.error("gysInfoDO 转 map 异常", e);
        }

        //自定义字段
        FormGroupFieldValueQuery formGroupFieldValueQuery = new FormGroupFieldValueQuery();
        formGroupFieldValueQuery.setSubjectId(gysInfoId);
        formGroupFieldValueQuery.setSubjectType("gys");
        List<FormGroupFieldValueDO> valueList = formGroupFieldValueService.selectDO(formGroupFieldValueQuery, siteId);

        //根据模板拼接参数
        for (FormGroupVO formGroupVO : form) {
            List<FormGroupFieldVO> fieldList = formGroupVO.getFormGroupFieldList();
            for (FormGroupFieldVO formGroupFieldVO : fieldList) {
                Long id = formGroupFieldVO.getId();
                String fieldName = formGroupFieldVO.getFieldCode();
                //添加系统字段的值
                Set set = map.entrySet();
                Iterator i = set.iterator();
                while(i.hasNext()){
                    Map.Entry<String, Object> entry = (Map.Entry<String, Object>)i.next();
                    if (StringUtils.isNotBlank(fieldName) && fieldName.equals(entry.getKey())) {
                        formGroupFieldVO.setContent((String) entry.getValue());
                        continue;
                    }
                }

                //添加自定义字段的值
                for (FormGroupFieldValueDO value : valueList) {
                    if (id.equals(value.getFieldId())) {
                        formGroupFieldVO.setContent(value.getContent());
                        continue;
                    }
                }
            }
        }

        return form;
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public List<GysInfoVO> batchSave(List<GysInfoAddDTO> gysInfoAddDTOList, TokenUser tokenUser) {
        if(CollectionUtils.isEmpty(gysInfoAddDTOList)) {
            return null;
        }
        List<GysInfoDO> gysInfoDOList = gysInfoAddDTOList
                .parallelStream()
                .map(gysInfoAddDTO -> {
                    GysInfoDO gysInfoDO = gysInfoAddDTO.to(GysInfoDO.class);
                    gysInfoDO.preInsert(tokenUser.getUserId());
                    gysInfoDO.setSiteId(tokenUser.getSiteId());
                    return gysInfoDO;
                }).collect(Collectors.toList());
        gysInfoDAO.insertListAndSetId(gysInfoDOList);
        return  gysInfoDOList
                .parallelStream()
                .map(gysInfoDO -> gysInfoDO.to(GysInfoVO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(GysInfoUpdateDTO gysInfoUpdateDTO, TokenUser tokenUser) {
        //转DO
        GysInfoDO gysInfoDO = gysInfoUpdateDTO.to(GysInfoDO.class);
        //根据主键更新，只更新非null值
        return updateByIdSelective(gysInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... gysInfoIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) gysInfoIds);
    }


    @Override
    public GysInfoVO selectById(String gysInfoId, String siteId) {
        GysInfoDO gysInfoDO = selectDOById(gysInfoId, siteId);
        if (gysInfoDO == null) {
            return null;
        }
        return gysInfoDO.to(GysInfoVO.class);
    }

    @Override
    public List<GysInfoListVO> select(GysInfoQuery gysInfoQuery, String siteId) {
        List<GysInfoDO> gysInfoDOList = selectDO(gysInfoQuery, siteId);
        if (CollectionUtils.isEmpty(gysInfoDOList)) {
            return gysInfoDOList == null ? null : new ArrayList<>();
        }
        return gysInfoDOList.stream()
                .map(gysInfoDO -> gysInfoDO.to(GysInfoListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(GysInfoQuery gysInfoQuery, String siteId) {
        Example example = buildExample(gysInfoQuery, siteId);
        return gysInfoDAO.selectCountByExample(example);
    }

    @Override
    public Page<GysInfoListVO> selectPage(GysInfoQuery gysInfoQuery, String siteId) {
//        Example example = buildExample(gysInfoQuery, siteId);
//        Page<GysInfoDO> page = gysInfoDAO.selectPageByExample(example,
//                gysInfoQuery.getPageNo(),
//                gysInfoQuery.getPageSize());

        PageHelper.startPage(gysInfoQuery.getPageNo(), gysInfoQuery.getPageSize());  //  设置分页值
        gysInfoQuery.setSiteId(siteId);
        List<GysInfoListVO> list = gysInfoDAO.selectGysInfo(gysInfoQuery);
        PageInfo<GysInfoListVO> pageInfo = new PageInfo<GysInfoListVO>(list);
        return new Page<>(list, pageInfo.getPageNum(), pageInfo.getPageSize(), (int)pageInfo.getTotal());
    }

    @Override
    public Page<Map> selectPageAllField(GysInfoQuery gysInfoQuery, TokenUser tokenUser) {
        //供应商主表分页数据
        Page<GysInfoListVO> selectPage = selectPage(gysInfoQuery, tokenUser.getSiteId());

        List<GysInfoListVO> list = selectPage.getEntities();
        List<String> gysIds = list.stream().map(GysInfoListVO::getId).collect(Collectors.toList());

        //查询自定义字段
        List<FormGroupFieldValueDO> valueList = formGroupFieldValueService.selectBySubject("gys", gysIds);

        //查询自定义表头
        HeaderFieldsQuery headerFieldsQuery = new HeaderFieldsQuery();
        headerFieldsQuery.setSubjectId(tokenUser.getSiteId());
        headerFieldsQuery.setModuleCode("gys");
        headerFieldsQuery.setCreateUserId(tokenUser.getUserId());
        FormHeaderVO formHeaderVO = formGroupService.getHead(headerFieldsQuery, tokenUser);
        List<HeaderFieldsListVO> canSeeList = formHeaderVO.getCanSee();
//        List<String> header = canSeeList.stream().map(HeaderFieldsListVO::getFieldEnName).collect(Collectors.toList());

        List<Map> result = new ArrayList<>();
        for (GysInfoListVO gysInfoListVO : list) {
            Map map = new HashMap();
            try {
                map = BeanCopyUtil.objectToMap(gysInfoListVO);
                String id = gysInfoListVO.getId();
                for (FormGroupFieldValueDO formGroupFieldValueDO : valueList) {
                    String gysId = formGroupFieldValueDO.getSubjectId();
                    if (id.equals(gysId)) {
                        map.put(String.valueOf(formGroupFieldValueDO.getFieldId()), formGroupFieldValueDO.getContent());
                    }
                }
            } catch (Exception e) {
                log.error("供应商对象转map异常", e);
            }

            //map 里面是所有的字段（系统 + 自定义），再根据header过滤一下(不进行过滤了，前端通过表头筛选就行)
//            Iterator iter = map.entrySet().iterator();
//            while (iter.hasNext()){
//                Map.Entry entry = (Map.Entry) iter.next();
//                Object key = entry.getKey();
//                if(key!=null){
//                    String curKey = key.toString();
//                    if(!header.contains(curKey)){
//                        iter.remove();  //移除出境 的专题
//                    }
//                }
//            }
            result.add(map);
        }

        Page<Map> page = new Page();
        try {
            org.springframework.beans.BeanUtils.copyProperties(selectPage, page);
            page.setEntities(result);
        } catch (Exception e) {
            log.error("供应商对象复制异常", e);
        }

        return page;
    }

    @Override
    public HeaderVo head(TokenUser tokenUser) {
        HeaderVo headerVo = new HeaderVo();

        List<List<String>> list = new ArrayList<List<String>>();
        List<String> enNameHeader = new ArrayList<String>();

        HeaderFieldsQuery headerFieldsQuery = new HeaderFieldsQuery();
        headerFieldsQuery.setModuleCode("gys");
        headerFieldsQuery.setCreateUserId(tokenUser.getUserId());
        HeaderFieldsVO headerFieldsVO = headerFieldsService.getHead(headerFieldsQuery, tokenUser);
        List<HeaderFieldsListVO> canSee = headerFieldsVO.getCanSee();

        canSee.sort(new Comparator<HeaderFieldsListVO>() {
            @Override
            public int compare(HeaderFieldsListVO o1, HeaderFieldsListVO o2) {
                return o1.getSort() - o2.getSort();
            }
        });

        for (HeaderFieldsListVO headerFieldsListVO : canSee) {
            List<String> head = new ArrayList<String>();
            head.add(headerFieldsListVO.getFieldName());
            list.add(head);
            enNameHeader.add(headerFieldsListVO.getFieldEnName());
        }

        headerVo.setHead(list);
        headerVo.setEnNameHeader(enNameHeader);
        return headerVo;
    }

    @Override
    public List<List<String>> allFieldValue(GysInfoQuery gysInfoQuery,
                                            List<String> enNameHeader,
                                            String siteId) {

        //供应商主表分页数据
        gysInfoQuery.setSiteId(siteId);
        List<GysInfoListVO> list = gysInfoDAO.selectGysInfo(gysInfoQuery);
        List<String> gysIds = list.stream().map(GysInfoListVO::getId).collect(Collectors.toList());

        //查询自定义字段
        List<FormGroupFieldValueDO> valueList = formGroupFieldValueService.selectBySubject("gys", gysIds);

        List<Map> result = new ArrayList<>();
        for (GysInfoListVO gysInfoListVO : list) {
            Map map = new HashMap();
            try {
                map = BeanCopyUtil.objectToMap(gysInfoListVO);
                String id = gysInfoListVO.getId();
                for (FormGroupFieldValueDO formGroupFieldValueDO : valueList) {
                    String gysId = formGroupFieldValueDO.getSubjectId();
                    if (id.equals(gysId)) {
                        map.put(String.valueOf(formGroupFieldValueDO.getFieldId()), formGroupFieldValueDO.getContent());
                    }
                }
            } catch (Exception e) {
                log.error("供应商对象转map异常", e);
            }
            result.add(map);
        }


        List<List<String>> data = new ArrayList<List<String>>();

        //循环map,每一个map对应一行数据，map中的每一项对于每一列
        for (Map map : result) {
            List<String> row = new ArrayList<String>();
            for (String enName : enNameHeader) {
                String value = (String) map.get(enName);
                row.add(value);
            }
            data.add(row);
        }

        return data;
    }

    @Override
    public GysInfoDO selectDOById(String gysInfoId, String siteId) {
        return listById(gysInfoId, siteId);
    }

    @Override
    public List<GysInfoDO> selectDO(GysInfoQuery gysInfoQuery, String siteId) {
        Example example = buildExample(gysInfoQuery, siteId);
        return gysInfoDAO.selectByExample(example);
    }

    @Override
    public List<GysInfoDO> selectGysByGroupIdList(List<String> childGroupIdList, TokenUser tokenUser) {
        Example example = new Example(GysInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andIn("groupId", childGroupIdList);
        return gysInfoDAO.selectByExample(example);
    }


    @Override
    public List<GysInfoExportVO> selectExportList(GysInfoQuery gysInfoQuery, String siteId) {
        int count = selectCount(gysInfoQuery,siteId);
        if (count > 10000) {
            throw new ParamException("一次最多导出10000条数据，请缩小检索范围");
        }
        List<GysInfoDO> gysInfoDOList = selectDO(gysInfoQuery,siteId);
        if (CollectionUtils.isEmpty(gysInfoDOList)) {
            return new ArrayList<>();
        }
        return gysInfoDOList.stream()
                .map(gysInfoDO -> gysInfoDO.to(GysInfoExportVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public List<ParseResultVO<GysInfoImportDTO>> parseImportFile(File file, Boolean success, TokenUser tokenUser) {
        GysInfoImportListener gysInfoImportListener = new GysInfoImportListener();
        gysInfoImportListener.setTokenUser(tokenUser);
        gysInfoImportListener.setParseOnly(true);
        // 读取excel 只解析不入库
        EasyExcel.read(file, GysInfoImportDTO.class, gysInfoImportListener).sheet().doRead();
        if (success == null) {
            return gysInfoImportListener.getParseResultList();
        } else {
            return gysInfoImportListener.getParseResultList()
                    .stream()
                    .filter(parseResult ->  success.equals(parseResult.getSuccess()))
                    .collect(Collectors.toList());
        }
    }

    @Override
    @Transactional( value = HrConstants.TX, rollbackFor = Exception.class)
    public Integer importFile(File file, TokenUser tokenUser, Boolean force) {
        GysInfoImportListener gysInfoImportListener = new GysInfoImportListener();
        gysInfoImportListener.setTokenUser(tokenUser);
        gysInfoImportListener.setForce(force);
        // 读取excel 并且入库
        EasyExcel.read(file, GysInfoImportDTO.class, gysInfoImportListener).sheet().doRead();
        return gysInfoImportListener.getSuccessCount();
    }

    @Override
    public Integer updateGsyInfo(GysUpdateDTO gysUpdateDTO, TokenUser tokenUser) {
        String groupId = gysUpdateDTO.getGroupId();
        List<GysInfoFieldAddDTO> list = gysUpdateDTO.getList();

        //供应商系统字段
        Map map = new HashMap();

        //获取自定义字段，保存自定义字段的值
        List<FormGroupFieldValueAddDTO> valueList = new ArrayList<FormGroupFieldValueAddDTO>();

        for (GysInfoFieldAddDTO gysInfoFieldAddDTO : list) {
            String enName = gysInfoFieldAddDTO.getEnName();
            if (StringUtils.isNotBlank(enName)) {
                map.put(enName, gysInfoFieldAddDTO.getContent());
            }
        }

        Integer result = 0;

        //保存供应商主表
        GysInfoDO gysInfoDO = new GysInfoDO();
        try {
            BeanUtils.populate(gysInfoDO, map);
        } catch (Exception e) {
            log.error("map 转DO异常：", e);
        }

        //校验供应商名称不能重复
        String gysName = gysInfoDO.getGysName();
        if (StringUtils.isNotBlank(gysName)) {
            Example example = new Example(GysInfoDO.class);
            example.and().andEqualTo("gysName",gysName)
                    .andEqualTo("siteId", tokenUser.getSiteId())
                    .andNotEqualTo("id", gysUpdateDTO.getId());
            List<GysInfoDO> gysList = gysInfoDAO.selectByExample(example);
            if (!CollectionUtils.isEmpty(gysList)) {
                throw new ParamException("供应商名称已存在");
            }
        }

        gysInfoDO.setId(gysUpdateDTO.getId());
        gysInfoDO.setGroupId(groupId);
        result = updateByIdSelective(gysInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());

        for (GysInfoFieldAddDTO gysInfoFieldAddDTO : list) {
            String enName = gysInfoFieldAddDTO.getEnName();
            if (StringUtils.isBlank(enName)) {
                //自定义字段，先查询是否存在，存在更新，不存在更新
                Long fieldId = gysInfoFieldAddDTO.getFieldId();
                String content = gysInfoFieldAddDTO.getContent();
                FormGroupFieldValueDO formGroupFieldValueDO = formGroupFieldValueService
                        .selectByFieldId(fieldId, gysInfoDO.getId(), FormGroupModuleCode.GYS);
                if (formGroupFieldValueDO != null) {
                    formGroupFieldValueDO.setContent(content);
                    formGroupFieldValueService.updateByIdSelective(formGroupFieldValueDO,
                            tokenUser.getSiteId(), tokenUser.getUserId());
                } else {
                    FormGroupFieldValueAddDTO formGroupFieldValueAddDTO = new FormGroupFieldValueAddDTO();
                    formGroupFieldValueAddDTO.setFieldId(fieldId);
                    formGroupFieldValueAddDTO.setContent(content);
                    formGroupFieldValueAddDTO.setSubjectType("gys");
                    formGroupFieldValueAddDTO.setSubjectId(gysInfoDO.getId());
                    formGroupFieldValueService.save(formGroupFieldValueAddDTO, tokenUser);
                }
            }
        }


        return result;
    }

    /**
     * 根据查询参数，构建example
     *
     * @param gysInfoQuery 查询参数
     * @param siteId       所属站点id
     * @return example
     */
    private Example buildExample(GysInfoQuery gysInfoQuery, String siteId) {
        Example example = new Example(GysInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (gysInfoQuery.getGroupId() != null) {
            example.and().andEqualTo("groupId", gysInfoQuery.getGroupId());
        }
        if (gysInfoQuery.getGysName() != null) {
            example.and().andEqualTo("gysName", gysInfoQuery.getGysName());
        }
        if (gysInfoQuery.getContent() != null) {
            example.and().andEqualTo("content", gysInfoQuery.getContent());
        }
        if (gysInfoQuery.getContractLife() != null) {
            example.and().andEqualTo("contractLife", gysInfoQuery.getContractLife());
        }
        if (gysInfoQuery.getAddress() != null) {
            example.and().andEqualTo("address", gysInfoQuery.getAddress());
        }
        if (gysInfoQuery.getRemark() != null) {
            example.and().andEqualTo("remark", gysInfoQuery.getRemark());
        }
        if (gysInfoQuery.getContractUrl() != null) {
            example.and().andEqualTo("contractUrl", gysInfoQuery.getContractUrl());
        }
        if (gysInfoQuery.getBusinessLicenseUrl() != null) {
            example.and().andEqualTo("businessLicenseUrl", gysInfoQuery.getBusinessLicenseUrl());
        }
        if (gysInfoQuery.getOtherFileUrl() != null) {
            example.and().andEqualTo("otherFileUrl", gysInfoQuery.getOtherFileUrl());
        }
        if (gysInfoQuery.getContactName() != null) {
            example.and().andEqualTo("contactName", gysInfoQuery.getContactName());
        }
        if (gysInfoQuery.getContactSex() != null) {
            example.and().andEqualTo("contactSex", gysInfoQuery.getContactSex());
        }
        if (gysInfoQuery.getContactPhone() != null) {
            example.and().andEqualTo("contactPhone", gysInfoQuery.getContactPhone());
        }
        if (gysInfoQuery.getContactEmail() != null) {
            example.and().andEqualTo("contactEmail", gysInfoQuery.getContactEmail());
        }
        if (gysInfoQuery.getContactWX() != null) {
            example.and().andEqualTo("contactWX", gysInfoQuery.getContactWX());
        }
        if (gysInfoQuery.getCorporateName() != null) {
            example.and().andEqualTo("corporateName", gysInfoQuery.getCorporateName());
        }
        if (gysInfoQuery.getCorporateSex() != null) {
            example.and().andEqualTo("corporateSex", gysInfoQuery.getCorporateSex());
        }
        if (gysInfoQuery.getCorporatePhone() != null) {
            example.and().andEqualTo("corporatePhone", gysInfoQuery.getCorporatePhone());
        }
        if (gysInfoQuery.getCorporateEmail() != null) {
            example.and().andEqualTo("corporateEmail", gysInfoQuery.getCorporateEmail());
        }
        if (gysInfoQuery.getCorporateWX() != null) {
            example.and().andEqualTo("corporateWX", gysInfoQuery.getCorporateWX());
        }
        //关键词搜素
        if (gysInfoQuery.getSearchKey() != null) {
            example.and(example.createCriteria()
                    .orLike("gysName", "%" + gysInfoQuery.getSearchKey() + "%")
                    .orLike("contactName", "%" + gysInfoQuery.getSearchKey() + "%")
                    .orLike("contactPhone", "%" + gysInfoQuery.getSearchKey() + "%"));
        }
        // 排序
        ExampleKit.setExampleOrder(example, gysInfoQuery.getOrders());
        return example;
    }
}




